﻿# -*- coding: utf-8 -*-


from django.db import models

## UserBase
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _l
from django.utils.translation import ugettext as _
## Cache object
import caching.base

# Create your models here.
# class User(User):
#
#     class Meta:
#         proxy = True
#
#     def active_subs(self):
#         return self.subs.filter(active=True).all()

#########
class VpnUser(caching.base.CachingMixin,models.Model):
    name = models.OneToOneField(User,verbose_name='用户',related_name="VpnUser")
    balance = models.FloatField(verbose_name='余额',default=10)
    total = models.FloatField(verbose_name='总额',default=0)
    address = models.CharField(max_length=128,default=" ",null=True,blank=True,verbose_name=_l("Address"))
    address2 = models.CharField(max_length=128,default=" ",null=True,blank=True,verbose_name=_l("Address2"))
    telephone = models.CharField(max_length=20,default=" ",null=True,blank=True,verbose_name=_l("Telephone"))
    zipcode = models.CharField(max_length=20,default=" ",null=True,blank=True,verbose_name=_l("Zipcode"))
    country = models.CharField(max_length=30,default=" ",null=True,blank=True,verbose_name=_l("Country"))
    province = models.CharField(max_length=30,default=" ",null=True,blank=True,verbose_name=_l("Province"))
    city = models.CharField(max_length=30,default=" ",null=True,blank=True,verbose_name=_l("City"))
    
    def __unicode__(self):
        return self.name.get_full_name()
    #
    # def count_vpn_account(self):
    #     i = 0
    #     i += self.ppp_account.all().count()
    #     return i

    class Meta:
        verbose_name = 'Vpn用户'




#############################

class Plan(caching.base.CachingMixin,models.Model):
    title = models.CharField(max_length=128,default=" ")
    body = models.TextField(default=" ",null=True,blank=True,verbose_name='说明')
    summary = models.TextField(default=" ",null=True,blank=True,verbose_name='简介')
    pic = models.CharField(max_length=128,default=" ")

    registration_cost = models.FloatField(default=0,verbose_name='首次费用')
    renew_cost = models.FloatField(default=0,verbose_name='续费费用')
    active = models.BooleanField(default=False)

    cycle = models.FloatField(default=1,verbose_name='续费周期,日')
    user_unique = models.IntegerField(default=-1,verbose_name='限购')


    def Monthly(self):
        return round(self.renew_cost / self.cycle * 30 , 2)

    def overage(self):
        return self.subscription_set.filter(user_id=1).all().count()


    def __unicode__(self):
        return self.title


    class Meta:
        verbose_name = '方案'




####################################################################################################################
def default_user():
    return User.objects.get(id=1)



def worldoverday():
    from datetime import datetime, time, date, timedelta
    d = date.today() + timedelta(days=9999)
    t = time(0, 0)
    return datetime.combine(d, t)

def worldstartday():
    from datetime import datetime, time, date, timedelta
    d = date.today() + timedelta(days=-9999)
    t = time(0, 0)
    return datetime.combine(d, t)



class Subscription(caching.base.CachingMixin,models.Model):
    plan = models.ForeignKey(Plan)
    user = models.ForeignKey(User,default=default_user,related_name="subs")
    cycle_start = models.DateTimeField(default=worldstartday)
    cycle_finish = models.DateTimeField(default=worldoverday)

    active = models.BooleanField(default=False)
    auto_renew = models.BooleanField(default=True)


    username = models.CharField(max_length=20,verbose_name="VPN帐号",unique=True)
    password = models.CharField(max_length=20,verbose_name="明文密码")
    status =  models.IntegerField(default=0,verbose_name="状态")
    clientip = models.CharField(max_length=32,verbose_name="客户端IP",unique=True)
    serverip = models.CharField(max_length=32,verbose_name="服务器IP",default="137.0.0.1")

    def abs_username(self):
        return self.username.split("@")[0]

    def expire(self):
        import datetime
        return self.cycle_finish < datetime.datetime.now()


    def used_percent(self):
        import datetime,time
        now =  float(time.mktime(datetime.datetime.now().timetuple()))
        finish = float(time.mktime(self.cycle_finish.timetuple()))
        start = float(time.mktime(self.cycle_start.timetuple()))
        return round(((now-start)/(finish-start))*100,1)


    class Meta:
        verbose_name = '订阅'
        get_latest_by = "id"







#########

#####

class Coupon(caching.base.CachingMixin,models.Model):
    start_date = models.DateTimeField(auto_now_add=True)
    code = models.CharField(max_length=128,verbose_name='号码')
    billing = models.FloatField(verbose_name='价值',default=0)
    status = models.IntegerField(verbose_name='状态',default=0)
    class Meta:
        verbose_name = '代金券'

